using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//挂在相机上
public class CRTEffect :MonoBehaviour{

	public Material m;
    [Range(0.0f,4.0f)]
    public float LuminanceThreshold = 0.6f;
  
//表面函数
    void Awake(){
     m = new Material(Shader.Find("Hidden/BWDiffuse"));
    }

    void OnRenderImage(RenderTexture src,RenderTexture dest){//渲染图像
    if(material !=null){
        //
        material.SetFloat("_LuminanceThreshold", LuminanceThreshold);
    
        int rtW = src.width/ downSample;
        int rtH = src.height/ downSample;

//定义亮度阈值
//第0个pass提取较亮区域
//第12个pass做高斯模糊
//第3个pass用相同uv对原始图像和灰度图像叠加
        RenderTexture buffer0 = RenderTexture.GetTemporary(rtW, rtH, 0);
        buffer0.filterMode = FilterMode.Point;//还原点采样有什么好处？ 
       
        Graphics.Blit(src, buffer0, material, 0);
        buffer0.filterMode = FilterMode.Point;

        RenderTexture buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);

        for(int i = 0; i < iterations; i++){

            material.SetFloat("_BlurSize", 1.0f + i * blurSpread);

//调用材质的第0个pass做垂直方向的模糊
            Graphics.Blit(buffer0, buffer1, material, 1);
//调用材质的第1个pass做水平方向的模糊
            Graphics.Blit(buffer1, buffer0, material, 2);
        }

        material.SetTexture("_Buffer1", buffer0);
        Graphics.Blit(src, dest, material, 3);

        RenderTexture.ReleaseTemporary(buffer0);
        RenderTexture.ReleaseTemporary(buffer1);
    }
    else{
        Graphics.Blit(src, dest);
    }
      
    }

}
